package Pet.Management.service;

import Pet.Management.domain.dto.PostDTO;
import Pet.Management.domain.entity.User;
import Pet.Management.domain.entity.CommunityPost;
import Pet.Management.repository.UserRepository;
import Pet.Management.repository.CommunityPostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PostService {

    @Autowired
    private CommunityPostRepository postRepository;
    
    @Autowired
    private UserRepository userRepository;

    /**
     * 获取帖子列表
     */
    public Page<PostDTO> getPostList(String title, String author, Pageable pageable) {
        Page<CommunityPost> postPage;
        
        if (title != null && !title.isEmpty() && author != null && !author.isEmpty()) {
            // 同时按标题和作者搜索
            postPage = postRepository.findByTitleContainingAndUserUsernameLike(title, "%" + author + "%", pageable);
        } else if (title != null && !title.isEmpty()) {
            // 按标题搜索
            postPage = postRepository.findByTitleContaining(title, pageable);
        } else if (author != null && !author.isEmpty()) {
            // 按作者搜索
            postPage = postRepository.findByUserUsernameLike("%" + author + "%", pageable);
        } else {
            // 获取所有帖子
            postPage = postRepository.findByDelFlagFalse(pageable);
        }

        List<PostDTO> postDTOs = postPage.getContent().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());

        return new PageImpl<>(postDTOs, pageable, postPage.getTotalElements());
    }

    /**
     * 删除帖子
     */
    @Transactional
    public boolean deletePost(Long postId) {
        try {
            // 获取帖子
            CommunityPost post = postRepository.findById(postId)
                    .orElseThrow(() -> new RuntimeException("帖子不存在"));
            
            // 逻辑删除
            post.setDelFlag(true);
            postRepository.save(post);
            
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 将实体转换为DTO
     */
    private PostDTO convertToDTO(CommunityPost post) {
        PostDTO dto = new PostDTO();
        dto.setId(post.getId());
        dto.setTitle(post.getTitle());
        dto.setContent(post.getContent());
        dto.setCreateTime(post.getCreateTime());
        dto.setViewCount(post.getViewCount());
        dto.setLikeCount(post.getLikeCount());
        dto.setCommentCount(post.getCommentCount());
        
        // 获取用户昵称
        User user = post.getUser();
        if (user != null) {
            dto.setNickName(user.getUsername());
            dto.setUserId(user.getId());
        }
        
        return dto;
    }
} 